package com.controller.student;

import com.bean.entity.*;
import com.bean.pojo.Dto;
import com.bean.pojo.PageAssistant;
import com.bean.util.DtoUtil;
import com.bean.vo.*;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private StudentScoreService studentScoreService;
    @Autowired
    private PhysicalHealthService physicalHealthService;
    @Autowired
    private DormitoryService dormitoryService;
    @Autowired
    private TransAddressService transAddressService;
    @Autowired
    private SubjectInfoService subjectInfoService;
    @Autowired
    private TeachersService teachersService;
    @Autowired
    private EvaluateService evaluateService;
    @Autowired
    private StudentCommentService studentCommentService;

    @RequestMapping("loginOff")
    public Dto studentLoginOff(HttpSession session){
        System.out.println("...学生退出");
        session.invalidate();

        return DtoUtil.returnSuccess("退出成功！");
    }

    //查询单个学生的方法暂时没用
//    @RequestMapping("queryStudent")
    public Dto teacherQueryStudent(StudentInfo studentInfo,HttpSession session){
        System.out.println("...查询登录的学生信息");
        System.out.println(studentInfo);

        try {
            List<StudentInfoVo> queryStudent = studentInfoService.query(studentInfo);
            session.setAttribute("studentLoginInfo",queryStudent);
            return DtoUtil.returnSuccess(queryStudent);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage());
        }
    }
    @RequestMapping("queryStuScore")
    public Dto queryScore(HttpServletRequest request,Integer page,Integer limit,PageAssistant<StudentScoreVo> pa, StudentScore studentScore){
        System.out.println("...查询个人学生成绩");
        Long id =(Long) request.getSession().getAttribute("studentLoginInfo");
        System.out.println("...登录的id"+id);
        pa.setCurrPage(page);
        pa.setPageSize(limit);

        Map<String,Object> map=new HashMap<String, Object>();

        map.put("studentId",id);
        PageAssistant<StudentScoreVo> studentScoreList = studentScoreService.queryByPage(map, pa);

        return DtoUtil.returnSuccess(studentScoreList);
    }

    @RequestMapping("queryStuPhy")
    public Dto queryPhy(HttpServletRequest request,Integer page, Integer limit, PageAssistant<PhysicalHealthVo> pa){
        System.out.println("...查询个人体质健康表");
        Long id =(Long) request.getSession().getAttribute("studentLoginInfo");

        System.out.println(id);

        //将框架的分页参数同步到分页助手
        pa.setCurrPage(page);
        pa.setPageSize(limit);

        //添加学生id—分页查询的参数
        Map<String,Object> map =new HashMap<String, Object>();
        map.put("studentId",id);

        PageAssistant<PhysicalHealthVo> stuPhy = physicalHealthService.queryByPage(map, pa);

        return DtoUtil.returnSuccess(stuPhy);

    }
    @RequestMapping("queryStuDormitory")
    public Dto queryDormitory(HttpServletRequest request, Integer page, Integer limit,PageAssistant<StudentInfoVo> pa){
        System.out.println("...查询学生宿舍");
        Long id =(Long) request.getSession().getAttribute("studentLoginInfo");

        pa.setCurrPage(page);
        pa.setPageSize(limit);

        Map<String,Object> map=new HashMap<String, Object>();
        map.put("studentId",id);

        //根据学生id查询宿舍
        try {
            List<StudentInfoVo> queryStuInfo = studentInfoService.query(new StudentInfo(id));

            Integer dormitoryId = queryStuInfo.get(0).getDormitoryId();

            //用宿舍的id去查宿舍名字
            List<DormitoryVo> dormitoryInfo = dormitoryService.query(new Dormitory(dormitoryId));
            String dormitoryName = dormitoryInfo.get(0).getDormitoryName();

            //用名字去查同一个宿舍的同学并返回宿舍的纯数字id
            List<DormitoryVo> sameDormitoryId = dormitoryService.query(new Dormitory(dormitoryName));
            System.out.println("同宿舍的宿舍信息"+sameDormitoryId);
            System.out.println("同宿舍的室友有"+sameDormitoryId.size()+"个");

            //用宿舍id去查询同宿舍的并返回
            List<StudentInfoVo> queryStudentInfoByDormitoryId=new ArrayList<StudentInfoVo>();
            for (int i = 0; i <sameDormitoryId.size() ; i++) {
                queryStudentInfoByDormitoryId.add(studentInfoService.query(new StudentInfo(sameDormitoryId.get(i).getId())).get(0));
            }
            //打印数据验证
            System.out.println("查询到的室友信息"+queryStudentInfoByDormitoryId);

            //将地址进行转化
            for (int i = 0; i <sameDormitoryId.size() ; i++) {
                if (queryStudentInfoByDormitoryId.get(i).getAddress()!=null){
                    queryStudentInfoByDormitoryId.get(i).setAddress(transAddressService.transAddress(queryStudentInfoByDormitoryId.get(i).getAddress()));
                }
                if (queryStudentInfoByDormitoryId.get(i).getBornplace()!=null){
                    queryStudentInfoByDormitoryId.get(i).setBornplace(transAddressService.transAddress(queryStudentInfoByDormitoryId.get(i).getBornplace()));
                }
                if (queryStudentInfoByDormitoryId.get(i).getNativePlace()!=null){
                    queryStudentInfoByDormitoryId.get(i).setNativePlace(transAddressService.transAddress(queryStudentInfoByDormitoryId.get(i).getNativePlace()));
                }
            }

            pa.setItems(queryStudentInfoByDormitoryId);
            return DtoUtil.returnSuccess(pa);
        } catch (Exception e) {
            e.printStackTrace();
            return  DtoUtil.returnFail(e.getMessage());
        }
    }

    @RequestMapping("queryMain")
    public Dto studentMain(HttpServletRequest request, HttpSession session, PageAssistant<StudentInfoVo> pa){
        Long id =(Long) request.getSession().getAttribute("studentLoginInfo");
        System.out.println("...查询登录者信息");
        System.out.println(id);

        try {
            List<StudentInfoVo> queryStudentById = studentInfoService.query(new StudentInfo(id));
//            List<StudentInfoVo> list=new ArrayList<StudentInfoVo>();
//            list.add(queryStudentById.get(0));
//            pa.setItems(list);

            for (int i = 0; i <queryStudentById.size() ; i++) {
                if (queryStudentById.get(i).getAddress()!=null){
                    queryStudentById.get(i).setAddress(transAddressService.transAddress(queryStudentById.get(i).getAddress()));
                }
                if (queryStudentById.get(i).getBornplace()!=null){
                    queryStudentById.get(i).setBornplace(transAddressService.transAddress(queryStudentById.get(i).getBornplace()));
                }
                if (queryStudentById.get(i).getNativePlace()!=null){
                    queryStudentById.get(i).setNativePlace(transAddressService.transAddress(queryStudentById.get(i).getNativePlace()));
                }
            }
            pa.setItems(queryStudentById);
            pa.setCount(queryStudentById.size());

            return DtoUtil.returnSuccess(pa);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage());
        }

    }

    //查找有缘人-包含同班同学，籍贯相同，居住地相同，生日同一个月份
    @RequestMapping("predestinedStudent")
    public Dto studentPredestined(PageAssistant<StudentInfoVo> pa,HttpServletRequest request){
        try {
            //获取登录的学生id
            Long loginStudentId=(Long)request.getSession().getAttribute("studentLoginInfo");
            //查询登录学生的信息
            List<StudentInfoVo> loginStudentInfo = studentInfoService.query(new StudentInfo(loginStudentId));
            //登录学生生日月份
            Integer loginBirthdayMonth = Integer.valueOf(loginStudentInfo.get(0).getCettificateNum().substring(10, 12));
            //登录学生班级id
            Long loginClassesId = loginStudentInfo.get(0).getClassesId();
            //登录学生居住地
            String loginAddress = loginStudentInfo.get(0).getAddress();

            //创建集合，存入对应的信息
            //创建同月出生的同学名字
            StringBuilder sameMonth=new StringBuilder();
            //创建同班同学的同学名字和个数
            StringBuilder sameClass=new StringBuilder();
            //创建居住地相同的信息
            StringBuilder sameAddress=new StringBuilder();

            List<StudentInfoVo> studentInfoVoList = studentInfoService.query(new StudentInfo());
            for (int i = 0; i <studentInfoVoList.size() ; i++) {
                if (studentInfoVoList.get(i).getCettificateNum()!=null){
                    //同学的生日-月份
                    Integer studentBirthdayMonth = Integer.valueOf(studentInfoVoList.get(i).getCettificateNum().substring(10,12));
                    if (loginBirthdayMonth.equals(studentBirthdayMonth)){
                        sameMonth.append(studentInfoVoList.get(i).getStudentName()+",");
                    }
                }
                if (studentInfoVoList.get(i).getClassesId()!=null){
                    //同学的班级id
                    Long studentClassesId = studentInfoVoList.get(i).getClassesId();
                    if (loginClassesId.equals(studentClassesId)) {
                        sameClass.append(studentInfoVoList.get(i).getStudentName()+",");
                    }
                }
                if (studentInfoVoList.get(i).getAddress()!=null){
                    //同学的居住地址
                    String studentAddress = studentInfoVoList.get(i).getAddress();
                    if (loginAddress.equals(studentAddress)){
                        sameAddress.append(studentInfoVoList.get(i).getStudentName()+",");
                    }
                }
            }
            System.out.println("同一个月出生的是"+sameMonth);
            System.out.println("同一个班级的同学是"+sameClass);
            System.out.println("同一个小区的同学是"+sameAddress);

            Map<String,Object> map=new HashMap<String, Object>();
            map.put("sameMonth",sameMonth);
            map.put("sameClass",sameClass);
            map.put("sameAddress",sameAddress);
            return DtoUtil.returnSuccess(map);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage());
        }
    }

    //理想学校
    @RequestMapping("dreamSchool")
    public Dto studentDreamSchool(HttpServletRequest request){
        System.out.println("...查询学校理想学校");
        Long loginStudentId = (Long)request.getSession().getAttribute("studentLoginInfo");

        try {
            StudentInfoVo loginStudent = studentInfoService.query(new StudentInfo(loginStudentId)).get(0);
            return DtoUtil.returnSuccess(loginStudent.getDreamSchool());
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage());
        }
    }

    @RequestMapping("querySubject")
    public Dto querySubject( ){
        System.out.println(">>>学生评教>>>查询科目");

        try {
            List<SubjectInfo> subjectInfoList = subjectInfoService.query(new SubjectInfo());
            return DtoUtil.returnSuccess(subjectInfoList);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage());
        }
    }

    @RequestMapping("queryTeacher")
    public Dto querySubject(SubjectInfo subjectInfo ){
        System.out.println(">>>学生评教>>>根据科目查询教师");

        try {
            List<TeachersVo> teachersVoList = teachersService.query(new Teachers(subjectInfo.getId(),null,null));
            return DtoUtil.returnSuccess(teachersVoList);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage());
        }
    }

    @RequestMapping("queryEvaluate")
    public Dto queryEvaluate(  ){
        System.out.println(">>>学生评教>>>查询评分等级");

        try {
            List<Evaluate> evaluateList = evaluateService.query(new Evaluate());
            return DtoUtil.returnSuccess(evaluateList);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage());
        }
    }

    @RequestMapping("addComment")
    public Dto addComment( HttpServletRequest request , StudentComment studentComment){
        System.out.println(">>>学生评教>>>提交");

        Long loginStudentId = (Long)request.getSession().getAttribute("studentLoginInfo");

        studentComment.setStudentId(loginStudentId);
        studentComment.setCommentTime(new Date());


        try {
            int save = studentCommentService.save(studentComment);
            return DtoUtil.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage());
        }


    }

}
